import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mahjong/const/MahjongSystemConst.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/page/mahjong/ExportCard.dart';
import 'SwapCustomerClip.dart';
import 'package:mahjong/entity/card/MahjongUserSwapCard.dart';

typedef CallBack = Function(AnimationController animationController);

class SwapCard extends StatefulWidget {
  final MahjongUserSwapCard mahjongUserSwapCard;
  final CallBack callBack;
  SwapCard(this.mahjongUserSwapCard, this.callBack);
  @override
  _SwapCardState createState() => _SwapCardState();
}

class _SwapCardState extends State<SwapCard>
    with SingleTickerProviderStateMixin {
  AnimationController _animationController;
  Animation _animation;
  double _height = MahjongSystemConst.exportAreaCardWidth *
      MahjongSystemConst.cardWHScale *
      3;
  bool handClick = false;

  @override
  void initState() {
    super.initState();
    _animationController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 500));
    _animation =
        Tween(begin: 0.0, end: MahjongSystemConst.exportAreaCardWidth * 4)
            .animate(_animationController);
    widget.callBack(_animationController);
    _animationController.addListener(() {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      left: 0,
      top: ScreenUtil().screenHeight -
          _height -
          MahjongSystemConst.bottomHandCardWidth *
              MahjongSystemConst.cardWHScale,
      child: SafeArea(
          child: Container(
        width: MahjongSystemConst.exportAreaCardWidth * 5,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            InkWell(
                onTap: () {
                  if (_animationController.isCompleted) {
                    _animationController.reverse();
                  } else {
                    _animationController.forward();
                  }
                },
                child: Container(
                    padding:
                        EdgeInsets.symmetric(horizontal: 1.h, vertical: 5.h),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: Colors.orange,
                        borderRadius: BorderRadius.circular(5.h)),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        tips("换"),
                        tips("牌"),
                        tips("信"),
                        tips("息"),
                      ],
                    ))),
            _animationController.isAnimating || _animationController.isCompleted
                ? ClipPath(
                    clipBehavior: Clip.antiAliasWithSaveLayer,
                    clipper: SwapCustomerClip(_animation.value, _height),
                    child: Container(
                        width: MahjongSystemConst.exportAreaCardWidth * 4,
                        height: _height,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: Colors.black.withOpacity(0.6),
                            borderRadius: BorderRadius.circular(5.0)),
                        clipBehavior: Clip.antiAliasWithSaveLayer,
                        child: Center(
                          child: widget.mahjongUserSwapCard == null
                              ? Text("暂无信息",
                                  style: TextStyle(
                                      fontSize: 14.sp,
                                      color: Colors.white,
                                      height: 1.0))
                              : Column(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceEvenly,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                      changeCard(
                                          "拿出:" +
                                              widget.mahjongUserSwapCard
                                                  .outSwapCard.userId,
                                          widget.mahjongUserSwapCard.outSwapCard
                                              .cards),
                                      changeCard(
                                          "拿进:" +
                                              widget.mahjongUserSwapCard
                                                  .inSwapCard.userId,
                                          widget.mahjongUserSwapCard.inSwapCard
                                              .cards),
                                    ]),
                        )))
                : SizedBox()
          ],
        ),
      )),
    );
  }

  Text tips(String str) {
    return Text(
      str,
      style: TextStyle(fontSize: 14.sp, color: Colors.white, height: 1.0),
    );
  }

  Column changeCard(String tips, List<int> cards) {
    return Column(children: [
      Text(
        tips,
        style: TextStyle(color: Colors.white, fontSize: 8.sp),
      ),
      Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: cards
            .map((item) => ExportCard(
                item, false, MahjongSystemConst.exportAreaCardWidth, false))
            .toList(),
      )
    ]);
  }
}
